import java.util.*;

/**
 * @author LKQ
 * @date 2022/4/25 17:21
 * @description 层序遍历 BFS
 */
public class Solution {
    public static void main(String[] args) {

    }
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> ans = new ArrayList<>();
        if (root == null) {
            return ans;
        }
        Queue<TreeNode> q = new LinkedList<>();
        q.add(root);
        while (!q.isEmpty()) {
            List<Integer> t = new ArrayList<>();
            int n = q.size();
            for (int i = 0; i < n; i++) {
                TreeNode p = q.poll();
                t.add(p.val);
                if (p.left != null) {
                    q.add(p.left);
                }
                if (p.right != null) {
                    q.add(p.right);
                }
            }
            ans.add(0, t);
        }
        return ans;
    }
}
